//
//  MTMTRecentlyCell.swift
//  MT
//
//  Created by ooops on 2020/6/1.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTMTRecentlyCell: UICollectionViewCell {
    
    private lazy var authProvider: MTProvider<DDCAuthAPI> = {
        let provider = MTProvider<DDCAuthAPI>()
        return provider
    }()
    
    /// 展示模型
    var model: HomeListDetailModel? {
        didSet {
            guard let model = model else { return }
            /// 封面图
            if let urlString = model.imgs?.first?.href {
                coverImageView.kf.setImage(with: URL(string: urlString), placeholder: UIImage(named: "placeholder-rectangle"))
            }
            
            headerImageView.kf.setImage(with: URL(string: model.headerUrl ?? ""))
            
            pauseIconButton.isHidden = !model.isContainVideo
            
            onlineStatusLabelLeft.text = model.onlineStatusRemark
        }
    }
    
    class func reuseIdentity() -> String {
        return "MTMTRecentlyCellID"
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.tag = 8800
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        imgView.clipsToBounds = true
//        let tap = UITapGestureRecognizer(target: self, action: #selector(homeListCell(didTapCoverImageView:)))
        //imgView.addGestureRecognizer(tap)
        return imgView
    }()
    
    /// 视频暂停 / 开始按钮
    lazy var pauseIconButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_start"), for: .normal)
        btn.setImage(UIImage(named: "icon_stop-1"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
//        btn.addTarget(self, action: #selector(homeListCell(didClickPlayPauseButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 是否在线
    lazy var onlineStatusLabelLeft: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0xffc000)
        label.backgroundColor = UIColor(hex:0x000000).withAlphaComponent(0.5)
        label.text = "15分钟前在线"
        label.textAlignment = .center
        return label
    }()
    
    /// 头像
    lazy var headerImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 10
        imgView.clipsToBounds = true
        return imgView
    }()
}

// MARK: ------------------ Overrides ----------------------
extension MTMTRecentlyCell {
    
    override func prepareForReuse() {
        pauseIconButton.isSelected = false
        coverImageView.image = nil
    }
    
}

// MARK: ------------------ Initialize ----------------------
extension MTMTRecentlyCell {
    
    /// UI 初始化
    private func initializeUI() {
        contentView.backgroundColor = UIColor(hex:0x000000)
        
        contentView.addSubview(coverImageView)
        coverImageView.addSubview(onlineStatusLabelLeft)
        coverImageView.addSubview(headerImageView)
    }
    
    /// 约束
    private func makeConstraints() {

        coverImageView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalToSuperview()
        }
        
        onlineStatusLabelLeft.snp.makeConstraints { (make) in
            make.top.left.equalTo(contentView).offset(4)
        }
        
        headerImageView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-4)
            make.left.equalToSuperview().offset(4)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
    }
    
}
